/**
 * 
 */
package cc.monggo.web.filter.response;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
 * @author fangjinsong To send character data, use the PrintWriter object
 *         returned by getWriter(). 截获打印流，即输出字符流
 * 
 */
public class PrintWriterResponseWrapper extends HttpServletResponseWrapper {
	private PrintWriter writer;// 打印机
	private ByteArrayOutputStream buffer;//缓冲区

	public PrintWriterResponseWrapper(HttpServletResponse response,ByteArrayOutputStream buffer)
			throws IOException {
		super(response);
		this.buffer = buffer;
	}

	@Override
	public void flushBuffer() throws IOException {
		writer.flush();
	}

	public void reset() {
		buffer.reset();
	}

	/**
	 * 此处一定要判断writer是否非空，否则产生错误
	 */
	@Override
	public PrintWriter getWriter() throws IOException {	
        if(writer == null) {  
        	writer = new PrintWriter(buffer);  
        }  
        return writer;  
	}

}
